# -*- coding: UTF-8 -*-
import esigntool1
import requests
from esigntool1 import esign_run_print_outer

config = esigntool1.config()  # 初始化配置类

orgName= 'bc4b12173a5c4c328123c0f16d986889'



@esign_run_print_outer
def main():
   # orgName='bc4b12173a5c4c328123c0f16d986889'
    #api_path = "/v3/sign-templates?orgId=".format(orgName)
    api_path="https://smlopenapi.esign.cn/v3/sign-templates?orgId=bc4b12173a5c4c328123c0f16d986889"
    # 签名并构造签名鉴权json请求头
    method = esigntool1.httpMethodEnum.GET
    json_heads= esigntool1.buildSignJsonHeader(config.appId, config.scert, method, api_path, body=None)
    print(json_heads)
    resp = requests.request(method, api_path, headers=json_heads)
    print(resp.text)
    return resp






@esign_run_print_outer
def accesstoken():
    api_path = "/v1/oauth2/access_token"
    method = esigntool1.httpMethodEnum.GET
    json_heads = {
        "X-Tsign-Open-App-Id": "application/x-www-form-urlencoded; charset=utf-8",
    }
    resp = requests.request(method, config.host + api_path, params={"grant_type": "client_credentials", "appId": config.appId, "secret": config.scert})
    print(resp.text)
    return resp

import json
import requests
from requests.auth import HTTPBasicAuth
from hashlib import md5
from datetime import datetime

class HttpSample:
    def __init__(self):
        self.base_url = "https://smlopenapi.esign.cn"
        self.app_id = "7439007392"
        self.auth_mode = "Signature"
        self.secret = "your_secret_key"  # 请替换为您的实际密钥
        self.charset = "UTF-8"

    def get_request(self, method, url, headers=None, data=None):
        if method.upper() == "GET":
            return requests.get(url, headers=headers, data=data)
        elif method.upper() == "POST":
            return requests.post(url, headers=headers, data=data)
        elif method.upper() == "HEAD":
            return requests.head(url, headers=headers, data=data)
        elif method.upper() == "OPTIONS":
            return requests.options(url, headers=headers, data=data)
        elif method.upper() == "PUT":
            return requests.put(url, headers=headers, data=data)
        elif method.upper() == "PATCH":
            return requests.patch(url, headers=headers, data=data)
        elif method.upper() == "DELETE":
            return requests.delete(url, headers=headers, data=data)
        elif method.upper() == "TRACE":
            return requests.trace(url, headers=headers, data=data)
        else:
            return requests.post(url, headers=headers, data=data)  # 默认使用POST

    def calculate_signature(self, method, uri, headers, body):
        accept = "*/*"
        content_type = "application/json; charset=UTF-8"
        date = str(int(datetime.now().timestamp() * 1000))
        content_md5 = md5(body.encode(self.charset) if body else "".encode(self.charset)).hexdigest()

        # 构建待签名字符串
        str_to_sign = f"{method}\n{accept}\n{content_md5}\n{content_type}\n{date}\n{uri}"
        signature = md5((str_to_sign + "\n" + self.secret).encode(self.charset)).hexdigest()
        return signature

    def main(self):
        url = f"{self.base_url}/v3/sign-templates?orgId=bc4b12173a5c4c328123c0f16d986889&status=1&pageNum=1&pageSize=20"
        method = "GET"

        # 构建请求体
        body = ""  # 如果有请求体，在这里设置
        uri = url[len(self.base_url):]  # 获取URI

        # 计算签名
        signature = self.calculate_signature(method, uri, {}, body)

        # 设置headers
        headers = {
            "Accept": "*/*",
            "Content-Type": "application/json; charset=UTF-8",
            "X-Tsign-Open-App-Id": self.app_id,
            "X-Tsign-Open-Auth-Mode": self.auth_mode,
            "X-Tsign-Open-Ca-Signature": signature,
            "X-Tsign-Open-Ca-Timestamp": str(int(datetime.now().timestamp() * 1000)),
            "Content-MD5": md5(body.encode(self.charset) if body else "".encode(self.charset)).hexdigest(),
        }

        # 发送请求
        #response = self.get_request(method, url, headers=headers, data=body)
        response = requests.request(method, url,headers=headers, data=body)

        # 打印响应码和结果
        print("responseCode:", response.status_code)
        print("result:", response.text)

if __name__ == "__main__":
    sample = HttpSample()
    sample.main()


HttpSample()